package com.ffcs.test.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 天真热
 * @create 2024-01-25 19:48
 * 流量流控
 * sentinel流控可以再客户端可视化界面实现，这里主要是为了实现代码流控
 * https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8
 * @desc
 **/
@RestController
@RequestMapping("/sentinel")
public class SentinelController_block {
    public final static String RESOURCE_NAME = "sentinel/block";
    public final static String RESOURCE_NAME1 = "sentinel/block1";


    /**
     * 流控规则
     */
    @PostConstruct
    private static void initFlowRules() {
        //流控规则
        List<FlowRule> rules = new ArrayList();

        //=========流控===========
        FlowRule rule = new FlowRule();
        //设置受保护的资源
        rule.setResource(RESOURCE_NAME);
        //设置流控规则QPS
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //设置受保护的资源阂值
        rule.setCount(1);
        rules.add(rule);

        //=========流控1===========
        FlowRule rule1 = new FlowRule();
        //设置受保护的资源
        rule1.setResource(RESOURCE_NAME1);
        //设置流控规则QPS
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //设置受保护的资源阂值
        rule1.setCount(1);
        rules.add(rule1);


        //加载配置好的规则
        FlowRuleManager.loadRules(rules);

    }
    //===========================================简单实现流控===============================================

    /**
     * 简单实现流控
     *
     * @return
     */
    @RequestMapping("/block")
    public String block() {
        Entry entry = null;
        try {
            // 1.sentinel针对资源进行限制的
            entry = SphU.entry(RESOURCE_NAME);//被保护的业务逻辑
            return "success";
        } catch (BlockException e1) {
            // 2.流控监听
            return "被流控了! ";
        } catch (Exception ex) {
            // 若需要配置降级规则，需要通过这种方式记录业务异常
            Tracer.traceEntry(ex, entry);
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
        return null;
    }


    //===========================================注解实现流控===============================================

    /**
     * 通过注解实现流控
     *
     * @return blockHandler+fallback
     * 1.函数访问范围需要是 public，返回类型需要与原方法相匹配，参数类型需要和原方法相匹配。blockHandler对应方法需要最后加一个额外的参数，类型为 BlockException。
     * 2.blockHandler 函数默认需要和原方法在同一个类中。
     * 3.若希望使用其他类的函数，则可以指定 blockHandlerClass 为对应的类的 Class 对象，注意对应的函数必需为 static 函数，否则无法解析。
     */
    @RequestMapping("/block1")
    @SentinelResource(value = RESOURCE_NAME1,
            blockHandlerClass = SentinelController_block.class, blockHandler = "blockHandlerMethod",
            fallbackClass = SentinelController_block.class, fallback = "fallbackMethod")
    public String block1() {
        int x = 1 / 0;
        return "111";
    }

    /**
     * 流控方法
     *
     * @return
     */
    public static String blockHandlerMethod(BlockException blockException) {
        return "触发流控";
    }

    /**
     * 异常方法
     *
     * @return
     */
    public static String fallbackMethod() {
        return "触发异常";
    }


}
